package com.lucaswangdev.logindemo.web;

import com.lucaswangdev.logindemo.constant.common.ErrorInfo;
import com.lucaswangdev.logindemo.exception.BusinessException;
import lombok.Data;

import java.io.Serializable;

@Data
public class BaseResult<T> implements Serializable {

    private static final long serialVersionUID = 4877838597686351074L;
    private boolean success;
    private String errorCode;
    private String errorMsg;
    private T data;

    public static <T> BaseResult<T> success() {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setSuccess(true);
        return baseResult;
    }

    public static <T> BaseResult<T> success(T data) {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setSuccess(true);
        baseResult.setData(data);
        return baseResult;
    }

    public static <T> BaseResult<T> bizFail(T data) {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setSuccess(false);
        baseResult.setData(data);
        return baseResult;
    }

    public static <T> BaseResult<T> bizFail(String errorCode, String errorMsg) {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setSuccess(false);
        baseResult.setErrorCode(errorCode);
        baseResult.setErrorMsg(errorMsg);
        return baseResult;
    }

    public static <T> BaseResult<T> bizFail(String errorCode) {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setSuccess(false);
        baseResult.setErrorCode(errorCode);
        return baseResult;
    }

    public static <T> BaseResult<T> bizFail(ErrorInfo errorInfo) {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setSuccess(false);
        baseResult.setErrorCode(errorInfo.getErrorCode());
        baseResult.setErrorMsg(errorInfo.getErrorMsg());
        return baseResult;
    }

    public static <T> BaseResult<T> bizFail(BusinessException businessException) {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setSuccess(false);
        baseResult.setErrorCode(businessException.getErrorCode());
        baseResult.setErrorMsg(businessException.getErrorMsg());
        return baseResult;
    }

    public static <T> BaseResult<T> systemFail() {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setSuccess(false);
        baseResult.setErrorCode("-1");
        baseResult.setErrorMsg("系统异常");
        return baseResult;
    }
}
